package com.gouaixin.bee.beeframework2.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.activity.paySuccessActivity;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2015/3/26.
 */
public class daojishi_fragment_huafei extends Fragment {
    View mainView;
    TextView d,H,m,s;

    long tomorrowData=0;



    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mainView=inflater.inflate(R.layout.fragment_chonghuafei,null);


        initView();



            // 通过Handler启动线程
            mHandler.post(mRunnable);

        return mainView;
    }

    private void removeFloatView() {
        WindowManager wm=(WindowManager)getActivity().getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
            wm.removeView(paySuccessActivity.myFV);
    }


    private void initData() {
        long between = getBetween();
        if(between>=00){
            long day = between / (24 * 60 * 60 * 1000);
            long hour = (between / (60 * 60 * 1000) - day * 24);
            long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
            long second = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
            long ms = (between - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
                    - min * 60 * 1000 - second * 1000);
            d.setText(day+"");
            H.setText(hour+"");
            m.setText(min+"");
            s.setText(second+"");
        }




    }

    private void initView() {
        d = (TextView) mainView.findViewById(R.id.tian);
        H = (TextView) mainView.findViewById(R.id.shi);
        m = (TextView) mainView.findViewById(R.id.fen);
        s = (TextView) mainView.findViewById(R.id.miao);



    }

    private Runnable mRunnable = new Runnable() {
        public void run() {
            // 更新界面
            mHandler.obtainMessage().sendToTarget();
            // 每1秒执行一次
            mHandler.postDelayed(mRunnable, 1000);
        }

    };

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            // 此处可以更新UI界面
            initData();
        }

    };



    /***
     * 获取第二天中午十二点的毫秒数 1。获取当时时间戳 2.获取当前年月日 3.将年月日拼接成明日字符串 4.就明日字符串转时间戳 5.计算时间戳差值
     * 6.获取年月日时分秒
     */
    public String jisuanTomorrow() {

//        long between = getBetween();
        long between = 1000*60*30;

        // 6.获取年月日时分秒
        long day = between / (24 * 60 * 60 * 1000);
        long hour = (between / (60 * 60 * 1000) - day * 24);
        long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        long ms = (between - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
                - min * 60 * 1000 - s * 1000);

        String result = day + "天" + hour + "小时" + min + "分" + s + "秒";

        return result;
    }

    /***
     * 得到间隔时间戳
     * @return
     */
    private long getBetween()  {
        long between=0;
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String result = "";
        // 1。获取当时时间戳
        Date date = new Date();

        long currentData = date.getTime();
//        android.util.Log.e("main", "当时时间戳" + currentData);

        // 2.获取当前年月日
//
//        Calendar cld = Calendar.getInstance();
//        int currentYear = cld.get(Calendar.YEAR);
////        android.util.Log.e("main", "年字符串" + currentYear);
//
//        int currentMonth = cld.get(Calendar.MONTH) + 1;
////        android.util.Log.e("main", "月字符串" + currentMonth);
//
//        long currentDay = cld.get(Calendar.DAY_OF_MONTH) + 1;
////        android.util.Log.e("main", "日字符串" + currentDay);
//
//        // 3.将年月日拼接成明日字符串
//        String tomorrowStr = currentYear + "-" + currentMonth + "-"
//                + currentDay + " " + "12:00:00.000";
////        android.util.Log.e("main", "明日字符串" + tomorrowStr);



        try {
//            tomorrowData = dfs.parse(tomorrowStr).getTime();
//            android.util.Log.e("main", "明日时间戳" + tomorrowData);
            // 5.计算时间戳差值
            tomorrowData= PreferencesUtils.getLong(getActivity(), "tomorrowData", 0);
            between = tomorrowData - currentData;// 得到两者的毫秒数
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return between;
    }

}
